<template>
  <div>
    <c-table
      border
      class="table-sm"
      header-cell-class-name="bg-light"
      :stripe="true"
      v-loading="loading"
      :columns="tableColumns"
      :data="tableData"
    ></c-table>
  </div>
</template>

<script>
export default {
  props: {
    data: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      order: {},
      params: {},
      loading: false,
      tableName: 'datum_customer_insurance_order_import_view',
      tableColumns: [
        { attrs: { label: '保单号', prop: 'insurance_order_no' }, process: v => v || '-' },
        { attrs: { label: '保险公司', prop: 'agency_name' }, process: v => v || '-' },
        { attrs: { label: '险种', prop: 'insurance_class_name' }, process: v => v || '-' },
        { attrs: { label: '保险金额(万元)', prop: 'ins_funds' }, process: v => v || '-' },
        { attrs: { label: '起始时间', prop: 'start_time' }, process: v => v || '-' },
        { attrs: { label: '终止时间', prop: 'end_time' }, process: v => v || '-' },
      ],
      tableData: [],
    }
  },

  computed: {
    isCompany() {
      return +this.data.customer_type === 1
    },
  },

  watch: {
    data: {
      handler: function () {
        if (!this.data.register_no && !this.data.id_card) {
          return
        }
        if (this.isCompany) {
          this.params['lender_idcard.eq'] = this.data.register_no
        } else {
          this.params['lender_idcard.eq'] = this.data.id_card
        }
        this.getData()
      },
      deep: true,
      immediate: true,
    },
  },
  created() {},
  methods: {
    getData() {
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params, this.order)
        .then(res => {
          this.tableData = ((res && res.data) || []).map(this.dataAdapter)
        })
        .finally(() => {
          this.loading = false
        })
    },
    dataAdapter(item) {
      item.start_time = this.$utils.dateFormat(item.start_time, 'yyyy年MM月dd日')
      item.end_time = this.$utils.dateFormat(item.end_time, 'yyyy年MM月dd日')
      return item
    },
  },
}
</script>
<style lang="scss" scoped></style>
